package org.dtrd.modules.doctor.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.config.system.Constant.USER_CATEGORY;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.doctor.entity.bean.DoctorTeam;
import org.dtrd.modules.doctor.entity.bean.TeamDoctorInfo;
import org.dtrd.modules.doctor.entity.bean.TeamInfo;
import org.dtrd.modules.doctor.entity.request.ManageTeamDoctorRequest;
import org.dtrd.modules.doctor.entity.request.SetTeamDoctorRequest;
import org.dtrd.modules.doctor.entity.request.UpdateTeamDoctorRequest;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorService;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorTeamService;
import org.dtrd.modules.doctor.service.IDtrdRlDoctorTeamDoctorService;
import org.dtrd.modules.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @author 萧
 * @date:2021-10-18 12:16 下午
 **/
@Api(tags = "医生团队")
@RestController
@Slf4j
@RequestMapping("/201/03")
public class DoctorTeamController extends BaseController {

    @Autowired
    private IDtrdRlDoctorTeamDoctorService doctorTeamRelationService;
    @Autowired
    private IDtrdEntRdDoctorTeamService doctorTeamService;
    @Autowired
    private IDtrdEntRdDoctorService doctorService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("获取医生的团队下所有医生的信息")
    @GetMapping("/2010301")
    public Result<?> getTeamDoctors(@RequestParam Integer doctorId, @RequestParam(required = false) String user) {
        if (!doctorService.isValidDoctorId(doctorId)) {
            return error(STATE_PARAMS_ERROR);
        }
        DoctorTeam doctorTeam = doctorTeamRelationService.getTeamDoctors(doctorId);
        if (USER_CATEGORY.USER_PATIENT.getUser().equals(user)) {
            if (doctorTeam != null) {
                List<TeamDoctorInfo> dtoList = new ArrayList<>(3);
                dtoList.add(doctorTeam.getManager());
                if (CollectionUtil.isNotEmpty(doctorTeam.getTeamDoctors())) {
                    dtoList.addAll(doctorTeam.getTeamDoctors());
                }
                doctorTeam.setTeamDoctors(dtoList);
            }
        }
        baseCommonService.addLog("获取医生的团队下所有医生的信息", LOG_TYPE_OPERATION, OPERATE_TYPE_QUERY, ShiroUtil.getLoginUser());
        return Result.OK(doctorTeam);
    }

    @ApiOperation("设置医生团队成员")
    @PostMapping("/2010302")
    public Result<?> saveDoctorTeamRelation(@RequestBody SetTeamDoctorRequest request) {
        // 空参数校验
        if (isIntParamInvalid(request.getTeamId())
                || StrUtil.isBlank(request.getDoctorPhone())
                || StrUtil.isBlank(request.getDoctorName())
                || request.getDoctorType() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        if (!Validator.isMobile(request.getDoctorPhone())) {
            return Result.error("手机号格式错误");
        }
        boolean result = doctorTeamRelationService.saveDoctorTeamRelation(request);
        baseCommonService.addLog("设置医生团队成员", LOG_TYPE_OPERATION, OPERATE_TYPE_SAVE_OR_UPDATE, ShiroUtil.getLoginUser());
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除医生团队成员")
    @PostMapping("/2010303")
    public Result<?> removeDoctorTeamRelation(@RequestBody ManageTeamDoctorRequest request) {
        Integer doctorId = request.getDoctorId();
        Integer teamId = request.getTeamId();
        if (isIntParamInvalid(teamId) || isIntParamInvalid(doctorId)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = doctorTeamRelationService.removeDoctorTeamRelation(doctorId, teamId);
        baseCommonService.addLog("删除医生团队成员", OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("查询医生的团队信息")
    @GetMapping("/2010304")
    @AutoLog(value = "查询医生的团队信息", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getTeamInfo(@RequestParam Integer teamId) {
        TeamInfo info = doctorTeamRelationService.getTeamInfo(teamId);
        return Result.OK(info);
    }

    @ApiOperation(value = "删除医生团队", notes = "删除团队的同会删除所有的团队关系记录")
    @GetMapping("/2010305")
    public Result<?> removeDoctorTeam(@RequestParam Integer teamId) {
        boolean result = doctorTeamRelationService.removeDoctorTeam(teamId);
        String logContent = String.format("删除医生团队, %d", teamId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation(value = "新增或修改医生团队信息", notes = "改的时候不能改管理者id")
    @PostMapping("/2010306")
    public Result<?> saveOrUpdateTeamInfo(@RequestBody TeamInfo info) {
        Integer managerId = info.getManagerId();
        if (info.getDataId() == null
                && managerId != null
                && doctorTeamService.hasDoctorTeam(managerId)) {
            return Result.error("该医生已有团队");
        }
        boolean result = doctorTeamService.saveOrUpdateDoctorTeam(info);
        String logContent = String.format("新增或修改医生团队信息, %s", info);
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("修改团队医生信息")
    @PostMapping("/2010307")
    public Result<?> updateTeamDoctorInfo(@RequestBody UpdateTeamDoctorRequest request) {
        if (request.getDoctorId() == null
                || StrUtil.isBlank(request.getDoctorPhone())
                || StrUtil.isBlank(request.getDoctorName())
                || request.getDoctorType() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        if (!doctorService.checkDoctorMobile(request.getDoctorPhone(), request.getDoctorId())) {
            return error(STATE_PHONE_REPETITION);
        }
        boolean result = doctorService.updateTeamDoctorInfo(request);
        String logContent = String.format("修改团队医生信息, %s", request);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
